
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
                  <el-form-item label="商品分类" prop="type">
                      <el-radio-group v-model="data.type">
                             <el-radio :label="1">实物</el-radio>   
                             <el-radio :label="2">虚拟</el-radio>
                             <el-radio :label="3">其他</el-radio>
                         </el-radio-group>
                    </el-form-item>
                        <el-form-item label="名称" prop="name">
                        <el-input v-model.trim="data.name" placeholder="请填写名称" />
                    </el-form-item>
                     <el-form-item label="描述" prop="desc">
                        <el-input type="textarea" v-model.trim="data.desc" placeholder="请填写描述"  rows="6" />
                    </el-form-item>
                    <el-form-item label="缩略图" prop="thumb" >
                        <el-upload
                    :action="$store.state.settings.file_upload_url"
                    :headers ="headerObj"
                    :limit=1
                    list-type="picture-card"
                    :file-list="data.thumbfileList"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                    :on-success="handlethumbSuccess"
                    :before-upload="beforethumbUpload"
                    :on-exceed="handlethumbExceed"
                    :on-preview="handlethumbPreview"
                    :on-remove="handlethumbRemove">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                    </el-form-item>
                    <el-form-item label="轮播图" prop="slide">
                        <el-upload
                    :action="$store.state.settings.file_upload_url"
                    :headers ="headerObj"
                    multiple
                    list-type="picture-card"
                    :on-preview="handleslidePreview"
                    :file-list="data.slidefileList"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                    :on-success="handleslideSuccess"
                    :on-remove="handleslideRemove"
                    >
                     <i class="el-icon-plus"></i>
                    </el-upload>
                    </el-form-item>
                    <el-form-item label="详情" prop="content">
                      <editor
                        v-model="data.content" v-if="this.item.dialogVisible"
                      />
                    </el-form-item>
                    <el-form-item label="市场价格" prop="out_price">
                        <el-input v-model.trim="data.out_price" placeholder="请填写市场价格" />
                    </el-form-item>
                    <el-form-item label="积分" prop="score">
                        <el-input v-model.trim="data.score" placeholder="请填写积分" />
                    </el-form-item>
                    <el-form-item label="是否显示">
                             <el-switch v-model="data.is_show"></el-switch>
                    </el-form-item>
                    <el-form-item label="库存" prop="stock">
                        <el-input-number v-model="data.stock" :min="1" :max="10" label="库存"></el-input-number>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="data.sort" :min="1" :max="10" label="排序"></el-input-number>
                    </el-form-item>

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/score'
import { getToken } from '@/utils/auth'
import  editor  from "@/components/Editor";


export default {
  name: 'scoreCreate',
  components: {editor },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
            name: [ 
                { required: true, message: "请输入名称", trigger: "blur" }
            ], 
            score: [ 
                { required: true, message: "请输入积分", trigger: "blur" }
            ], 

      },
      data: {
            name:'',
            type:1,
            desc:'',
            thumb:[],
            thumbpreviewurl:'',
            thumbfileList:[],
            slide:[],
            slidepreviewurl:'',
            slidefileList:[],
            content:'',
            out_price:'',
            score:'',
            is_show:true,
            stock:'',
            sort:'',

      }
    }
  },
  created() {
  },
  methods: {
    
                handlethumbExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handlethumbPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handlethumbRemove(){
                 this.data.thumb = '';
             },
             handlethumbSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.thumb= response.data.src;
            },
             beforethumbUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                 return this.$message.error("上传图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                  return  this.$message.error("上传图片大小不能超过 2MB!");
                }
                
            },
             handleslidePreview(file) {
                this.dialogVisible = true;
                this.previewUrl = file.url;
            },
             handleslideSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                 this.data.slide.push(response.data.src);
            },
             beforeslideUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                  return this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                  return  this.$message.error("上传头像图片大小不能超过 2MB!");
                }
                
            },
             handleslideRemove(file,fileList) {
                const that = this;
                let index = this.data.slidefileList.findIndex(
                  (item) => item.url == file.url
                ); // 要删除的数组下标
                if (index != -1) {
                  that.data.slidefileList.splice(index, 1);
                  that.data.slide.splice(index, 1);
                }
            },
             handleInput(value) {
            },
             handleClick(e, editor) {
            },
             clear() {
                this.$refs.editor.clear()
            },

    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create({
          type : this.data.type,
            name:this.data.name,
            desc:this.data.desc,
            thumb:this.data.thumb,
            slide:this.data.slide.join(','),
            content:this.data.content,
            out_price:this.data.out_price,
            score:this.data.score,
            is_show:this.data.is_show ?1:0,
            stock:this.data.stock,
            sort:this.data.sort,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
